/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ontology;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;

/**
 *
 * @author livia
 */
public class Ontology {
    //physical uri
    public URI uriLocation;
    // ontologia da owl api
    public  OWLOntology owl;
    // conjunto de classes da ontologia
    private HashMap<String, OWLClass> classes;
    // conjunto de propriedades de dados da ontologia
    private HashMap<String, OWLDataProperty> dataProperties;
    // conjunto de propriedades de objeto da ontologia
    private HashMap<String, OWLObjectProperty> objectPeoperties;
    // conjunto de propriedades dados cuja a classe chave eh o dominio
    private HashMap<String, ArrayList<OWLDataProperty>> dataPropertiesOfClass;
    // conjunto de propriedades objetos cuja a classe chave eh o dominio
    private HashMap<String, ArrayList<OWLObjectProperty>> objectPropertiesOfClass;
    // conjunto de todos os recursos : propriedades de dados, propriedades de objetos e classes

    public Ontology(URI ontologyURI) throws OWLOntologyCreationException{
         OWLOntologyManager manager1 = OWLManager.createOWLOntologyManager();
         System.out.println("uri : "+ontologyURI+"");
         owl = manager1.loadOntologyFromPhysicalURI(ontologyURI);
         uriLocation = ontologyURI;
         init();         
    }
    
    /*public Ontology(OWLOntology ontology) {
        this.owl = ontology;
        init();
    }*/

    private void init(){
        this.classes = new HashMap<String, OWLClass>();
        this.dataProperties = new HashMap<String, OWLDataProperty>();
        this.objectPeoperties = new HashMap<String, OWLObjectProperty>();
        this.dataPropertiesOfClass = new HashMap<String, ArrayList<OWLDataProperty>>();
        this.objectPropertiesOfClass = new HashMap<String, ArrayList<OWLObjectProperty>>();

        Set<OWLClass> clss = owl.getReferencedClasses();
        Set<OWLDataProperty> dataprops = owl.getReferencedDataProperties();
        Set<OWLObjectProperty> objprops = owl.getReferencedObjectProperties();
        Set<OWLDescription> domainsDt;
        Set<OWLDescription> domainsObj;

         System.out.println("clss "+clss);

        // inclui classe da ontologia
        for (OWLClass cl: clss){
            classes.put(cl.toString(), cl);
            dataPropertiesOfClass.put(cl.toString(),new ArrayList<OWLDataProperty>());
            objectPropertiesOfClass.put(cl.toString(), new ArrayList<OWLObjectProperty>());
        }

        // inclui propriedades de dados na ontologia
        for (OWLDataProperty dt: dataprops){
            this.dataProperties.put(dt.toString(), dt);
            domainsDt = dt.getDomains(owl);
            for (OWLDescription cl : domainsDt) {
                System.out.println("data properties "+dataPropertiesOfClass);
                if(cl instanceof OWLClass)dataPropertiesOfClass.get(cl.toString()).add(dt);
            }
        }
        // inclui propriedades de objetos na ontologia
        for (OWLObjectProperty obj: objprops){
            this.objectPeoperties.put(obj.toString(), obj);
            domainsObj = obj.getDomains(owl);
            for (OWLDescription cl : domainsObj) {
                objectPropertiesOfClass.get(cl.asOWLClass().toString()).add(obj);
            }
        }

        System.out.println("classes "+classes);
        System.out.println("objetos "+objectPeoperties);
        System.out.println("data "+dataProperties);
    }

    public OWLClass getClass(String className){
       return classes.get(className);
    }

    public Set<String> getClassesName(){
        return  classes.keySet();
    }

    public Set<String> getDataPropName(){
        return  dataProperties.keySet();
    }

     public Set<String> getObjPropName(){
        return  objectPeoperties.keySet();
    }

    public OWLDataProperty getDataProperty(String dtPropName){
      return dataProperties.get(dtPropName);
    }

    public OWLObjectProperty getObjectProperty(String objPropName){
      return objectPeoperties.get(objPropName);
    }

    public ArrayList<OWLDataProperty> getDataPropertiesOf(String cls){
        return dataPropertiesOfClass.get(cls);
    }

    public ArrayList<OWLObjectProperty> getObjPropertiesOf(String cls){
        return objectPropertiesOfClass.get(cls);
    }

    public Object getResource(String resourceName) {
        Object res;
        if((res=getClass(resourceName))!=null)return res;
        if((res=getObjectProperty(resourceName))!=null)return res;
        return getDataProperty(resourceName);
    }
   
}
